﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmThongKeMT : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmThongKeMT()
        {
            InitializeComponent();
        }
        ConnectData conn=new ConnectData();
        Utilities uti=new Utilities();
        private void FrmThongKeMT_Load(object sender, EventArgs e)
        {
            //Load combobox nhom ban doc
            DataTable tbNhom = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_MaNhom.DataSource = tbNhom;
            DataRow drLoaiSach = tbNhom.NewRow();
            drLoaiSach["MaNhom"] = "all";
            drLoaiSach["TenNhom"] = "Tất cả";
            tbNhom.Rows.InsertAt(drLoaiSach, 0);
            comb_MaNhom.DisplayMember = "TenNhom";
            comb_MaNhom.ValueMember = "MaNhom";
            comb_MaNhom.SelectedIndex = 0;
            //
            comb_TinhTrang.Items.Add("Tất cả");
            comb_TinhTrang.Items.Add("Đã trả");
            comb_TinhTrang.Items.Add("Chưa trả");
            comb_TinhTrang.SelectedIndex = 0;
            //
            comb_TT.Items.Add("Tất cả");
            comb_TT.Items.Add("Đã gia hạn");
            comb_TT.Items.Add("Quá hạn");
            comb_TT.SelectedIndex = 0;
            //DATETIME
            date_MuonDen.Value = System.DateTime.Now;
            date_MuonTu.Value = System.DateTime.Now;
            date_TraDen.Value = System.DateTime.Now;
            date_TraTu.Value = System.DateTime.Now;
            date_MuonDen.Enabled = false;
            date_MuonTu.Enabled = false;
            date_TraDen.Enabled = false;
            date_TraTu.Enabled = false;
        }
        string getSQL(string TenBC)
        {
            string sql = "select e.MaMuonTra, MaCTMuonTra,a.MaBanDoc,TenBanDoc, a.MaNhom, TenNhom,c.MaBanSach, TenDauSach, NgayMuon, NgayPhaiTra, NgayTra,NgayGiaHan,N'"+TenBC+"' TenBC " +
                         "from BanDoc a, NhomBanDoc b, BanSach c, DauSach d, PhieuMuonTra e, CTMuonTra f " +
                         "where a.MaNhom=b.MaNhom and c.MaDauSach=d.MaDauSach and e.MaMuonTra=f.MaMuonTra and a.MaBanDoc=e.MaBanDoc and c.MaBanSach=f.MaBanSach ";
            if (comb_MaNhom.SelectedIndex != 0)
                sql += "and a.MaNhom=N'"+comb_MaNhom.SelectedValue.ToString()+"' ";
            if(txt_MaBanDoc.Text!="")
                sql+="and a.MaBanDoc like N'"+txt_MaBanDoc.Text+"'";
            if (comb_TinhTrang.SelectedIndex == 1)//Đã trả
                sql += "and NgayTra is not null ";
            if (comb_TinhTrang.SelectedIndex == 2)//Chưa trả
                sql += "and NgayTra is null ";
            if (comb_TT.SelectedIndex == 1)//Gia hạn
                sql += "and NgayGiaHan is not null ";
            if (comb_TT.SelectedIndex == 2) //QUá hạn
                sql += "and ((NgayTra is not null and NgayTra>NgayPhaiTra) or (NgayTra is null and NgayPhaiTra<GETDATE()))";

            //Ngày tháng mượn
            if (check_MuonTu.Checked == true && check_MuonDen.Checked == false)
                sql += "and NgayMuon>='" + date_MuonTu.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_MuonTu.Checked == false && check_MuonDen.Checked == true)
                sql += "and NgayMuon <='" + date_MuonDen.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_MuonTu.Checked == true && check_MuonDen.Checked == true)
                sql += "and NgayMuon BETWEEN '" + date_MuonTu.Value.ToString("yyyy-MM-dd") + "' and '" + date_MuonDen.Value.ToString("yyyy-MM-dd") + "'";
            //Ngáy tháng trả
            if (check_TraTu.Checked == true && check_TraDen.Checked == false)
                sql += "and NgayTra>='" + date_TraTu.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_TraTu.Checked == false && check_TraDen.Checked == true)
                sql += "and NgayTra <='" + date_TraDen.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_TraTu.Checked == true && check_TraDen.Checked == true)
                sql += "and NgayTra BETWEEN '" + date_TraTu.Value.ToString("yyyy-MM-dd") + "' and '" + date_TraDen.Value.ToString("yyyy-MM-dd") + "'";
            return sql;
        }
        
        private void btn_XemKq_Click(object sender, EventArgs e)
        {
            dgv_HienThi.DataSource = conn.GetDataTable(getSQL(txt_TenBC.Text.ToUpper()));
        }

        private void btn_In_Click(object sender, EventArgs e)
        {
            if (txt_TenBC.Text != "")
            {
                FrmReport_MT frm = new FrmReport_MT();
                frm.dtb = conn.GetDataTable(getSQL(txt_TenBC.Text.ToUpper()));
                frm.dk = 2;
                frm.ShowDialog();
            }
            else
                MessageBox.Show("Chưa nhập tiêu đề báo cáo", "Thông báo");
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void check_MuonTu_CheckedChanged(object sender, EventArgs e)
        {
            if (check_MuonTu.Checked == true)
                date_MuonTu.Enabled = true;
            else
                date_MuonTu.Enabled = false;
        }

        private void check_MuonDen_CheckedChanged(object sender, EventArgs e)
        {
            if (check_MuonDen.Checked == true)
                date_MuonDen.Enabled = true;
            else
                date_MuonDen.Enabled = false;
        }

        private void check_TraTu_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TraTu.Checked == true)
                date_TraTu.Enabled = true;
            else
                date_TraTu.Enabled = false;
        }

        private void check_TraDen_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TraDen.Checked == true)
                date_TraDen.Enabled = true;
            else
                date_TraDen.Enabled = false;
        }

        private void dgv_HienThi_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_HienThi);
        }

    }
}
